package com.coincalf.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.coincalf.entity.UserFavoriteMarket;
import com.coincalf.feign.MarketServiceClient;
import com.coincalf.framework.exception.GlobalDefaultException;
import com.coincalf.framework.http.Response;
import com.coincalf.framework.security.UserDetails;
import com.coincalf.service.UserFavoriteMarketService;
import com.coincalf.web.vo.UserFavoriteMarketForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@RestController
@RequestMapping("/favorite")
@Api(value = "用户收藏交易市场", description = "用户收藏交易市场")
public class UserFavoriteMarketController {


    @Autowired
    private MarketServiceClient marketServiceClient;
    @Autowired
    private UserFavoriteMarketService userFavoriteMarketService;
    @Autowired
    private CommonController commonController;


    @PostMapping("/addFavorite")
    @ApiOperation(value = "添加用户交易市场", notes = "添加用户交易市场", httpMethod = "POST")
    //@ApiOperation(value = "添加用户交易市场", notes = "添加用户交易市场", httpMethod = "POST", authorizations = {@Authorization(value = "Authorization")})
    public Object addFavorite(HttpServletRequest request, @RequestBody UserFavoriteMarketForm form) {
        Long userId = commonController.getUserId(request);
        long marketId = marketServiceClient.getBySymbol(form.getSymbol());
        if (marketId == 0L) {
            throw new GlobalDefaultException(10002);
        }
        //查找交易市场是否收藏
        EntityWrapper<UserFavoriteMarket> e = new EntityWrapper<>();
        e.eq("market_id", marketId);
        e.eq("user_id", userId);
        UserFavoriteMarket f = userFavoriteMarketService.selectOne(e);
        if (Optional.ofNullable(f).isPresent()) {
            throw new GlobalDefaultException(20011);
        }

        UserFavoriteMarket u = new UserFavoriteMarket();
        u.setMarketId(marketId).setType(form.getType()).setUserId(userId);
        userFavoriteMarketService.insert(u);
        return Response.ok();
    }

    @DeleteMapping("/deleteFavorite/{symbol}/{type}")
    @ApiOperation(value = "撤销用户交易市场", notes = "撤销用户交易市场", httpMethod = "DELETE")
    //@ApiOperation(value = "撤销用户交易市场", notes = "撤销用户交易市场", httpMethod = "DELETE", authorizations = {@Authorization(value = "Authorization")})
    public Object deleteFavorite(HttpServletRequest request, @PathVariable String symbol, @PathVariable long type) {
        Long userId = commonController.getUserId(request);
        long marketId = marketServiceClient.getBySymbol(symbol);
        EntityWrapper<UserFavoriteMarket> e = new EntityWrapper<>();
        e.eq("market_id", marketId);
        e.eq("type", type);
        e.eq("user_id", userId);
        userFavoriteMarketService.delete(e);
        return Response.ok();
    }

}
